<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>签到/签退</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="../css/mui.min.css">
		<!--App自定义的css-->
		<!-- <link rel="stylesheet" type="text/css" href="../css/app.css" /> -->
		<style>
			h5 {
				margin: 5px 7px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">签到/签退</h1>
		</header>
		<div class="mui-content" style="">
			<div id="scan" style="width: 100%;height: 700px;"></div>
			<div id="form" class="mui-content-padded" style="margin: 5px;display: none;">
				<form class="mui-input-group">
					<input id="signInId" type="hidden"/>
					<div class="mui-input-row">
						<label>姓名</label>
						<input id="name" type="text" readonly>
					</div>
					<div class="mui-input-row">
						<label>电话</label>
						<input id="loginname" type="text" readonly>
					</div>
					<div class="mui-input-row">
						<label>部门</label>
						<input id="organizationName" type="text" readonly>
					</div>
					<div class="mui-input-row">
						<label>签到</label>
						<input id="upRet" type="text" readonly>
					</div>
					<div class="mui-input-row">
						<label>签到时间</label>
						<input id="upTime" type="text" readonly>
					</div>
					<div class="mui-input-row">
						<label>签退</label>
						<input id="downRet" type="text" readonly>
					</div>
					<div class="mui-input-row">
						<label>签退时间</label>
						<input id="downTime" type="text" readonly>
					</div>
					<div id="buttonDiv" class="mui-button-row" style="display: none;">
						<button id="toUp" style="display: none;" type="button" class="mui-btn mui-btn-success">签到</button>&nbsp;&nbsp;
						<button id="toDown" style="display: none;" type="button" class="mui-btn mui-btn-primary">签退</button>&nbsp;&nbsp;
						<button id="errToDown" style="display: none;" type="button" class="mui-btn mui-btn-danger">早退</button>&nbsp;&nbsp;
					</div>
				</form>
			</div>
		</div>
		<script type="text/javascript" src="../js/jquery1113.js"></script>
		<script src="../js/mui.min.js"></script>
		<script src="../js/util.js"></script>
		<script src="../js/data.js"></script>
		<script type="text/javascript" src="../js/QRCode.js"></script>
		<script>
			mui.init({
				swipeBack: true //启用右滑关闭功能
			});
			mui.plusReady(function() {
				barcodeStart(onmarked);
				document.getElementById("errToDown").addEventListener('click', function() {
					var btnArray = ['取消', '早退'];
					mui.confirm('还未到规定时间签退，现在签退视为早退', '早退警告', btnArray, function(e) {
						if (e.index == 1) {
							signInServer()
						}
					})
				});
				document.getElementById("toUp").addEventListener('click', function() {
					signInServer()
				});
				document.getElementById("toDown").addEventListener('click', function() {
					signInServer()
				});
			});
			
			var commitFlag = true;
			
			function onmarked(type, result) {
				if(type == plus.barcode.QR){
					getSigIn(result);
				}
			}
			function getSigIn(code){
				var name = plus.storage.getItem('name');
				var password = plus.storage.getItem('password');
									
				if(name != null && name != '' && password != null && password != ''){
					
					var jsonObject = {};
					jsonObject['name'] = name;
					jsonObject['password'] = password;
					jsonObject['stampId'] = code;
					
					xhr = new plus.net.XMLHttpRequest();
					xhr.onreadystatechange = function() {
						if(xhr.readyState == 4 && xhr.status == 200){
							var data = JSON.parse(xhr.responseText);
							if (data && data.result == 1 && data.data) {
								var signIn = data.data;
								$("#signInId").val(signIn.id);
								$("#name").val(signIn.user.name);
								$("#loginname").val(signIn.user.loginname);
								$("#organizationName").val(signIn.organizationName);
								if(signIn.upRet == 1){
									$("#upRet").val("已签到");
								} else if(signIn.upRet == 0){
									$("#upRet").val("未签到");
								} else if(signIn.upRet == 2){
									$("#upRet").val("迟到");
								}
								if(signIn.downRet == 1){
									$("#downRet").val("已签退");
								} else if(signIn.downRet == 0){
									$("#downRet").val("未签退");
								} else if(signIn.downRet == 2){
									$("#downRet").val("早退");
								}
								$("#upTime").val(signIn.upTime);
								$("#downTime").val(signIn.downTime);
								
								if(signIn.upRet == 0){
									$("#buttonDiv").show();
									//签到
									$("#toUp").show();
								} else if(signIn.downRet == 0){
									$("#buttonDiv").show();
									//签退
									var teachClass = data.teachClass;
									console.log(teachClass)
									var options = {
									  timeZone: 'Asia/Shanghai',
									  hour12: false,
									  hour: '2-digit',
									  minute: '2-digit',
									  second: '2-digit'
									};
									var downTimeRule = Number(teachClass.downTime.replaceAll(":",""));
									var nowTime = Number(new Date().toLocaleString('en-US', options).replaceAll(":",""))
									if(nowTime < downTimeRule) {
										//早退
										$("#errToDown").show();
									} else {
										//正常签退
										$("#toDown").show();
									}
								}
								barcodeClose();
								$("#scan").hide();
								$("#form").show();
							} else {
								mui.alert(data.message);
							}
						}
					}
					xhr.open("POST", serverUrl + "signIn/PubGetSignInByStampId.do");
					xhr.setRequestHeader("Content-Type", "application/json");
					xhr.send(JSON.stringify(jsonObject));
				}
			}
			function signInServer(){
				if(!commitFlag){
					return;
				}else{
					commitFlag = false;
				}
				$("#buttonDiv").hide();
				var name = plus.storage.getItem('name');
				var password = plus.storage.getItem('password');
				var signInId = $("#signInId").val();
									
				if(name != null && name != '' && password != null && password != '' && signInId != null && signInId != ''){
					
					var jsonObject = {};
					jsonObject['name'] = name;
					jsonObject['password'] = password;
					jsonObject['signInId'] = signInId;
					
					xhr = new plus.net.XMLHttpRequest();
					xhr.onreadystatechange = function() {
						if(xhr.readyState == 4 && xhr.status == 200){
							var data = JSON.parse(xhr.responseText);
							if (data && data.result == 1 && data.data) {
								var signIn = data.data;
								$("#signInId").val(signIn.id);
								$("#name").val(signIn.user.name);
								$("#loginname").val(signIn.user.loginname);
								$("#organizationName").val(signIn.organizationName);
								if(signIn.upRet == 1){
									$("#upRet").val("已签到");
								} else if(signIn.upRet == 0){
									$("#upRet").val("未签到");
								} else if(signIn.upRet == 2){
									$("#upRet").val("迟到");
								}
								if(signIn.downRet == 1){
									$("#downRet").val("已签退");
								} else if(signIn.downRet == 0){
									$("#downRet").val("未签退");
								} else if(signIn.downRet == 2){
									$("#downRet").val("早退");
								}
								$("#upTime").val(signIn.upTime);
								$("#downTime").val(signIn.downTime);
							} else {
								mui.alert(data.message);
							}
						}
					}
					xhr.open("POST", serverUrl + "signIn/PubToSignIn.do");
					xhr.setRequestHeader("Content-Type", "application/json");
					xhr.send(JSON.stringify(jsonObject));
				}
			}
		</script>
	</body>

</html>